/**
 * 
 */
package image.header;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @author nquangtrung
 *
 */
public class DIBHeader {
	private int size;
	private int width;
	private int height;
	private short depth;
	private short colorPlane;
	private int compression;
	private int imageSizeWithPadding;

	/**
	 * 
	 */
	public DIBHeader() {
		
	}
	
	public void parse(int size, byte[] dibBit) {
		this.setSize(size);
		ByteBuffer bb = ByteBuffer.wrap(dibBit);
		bb.order(ByteOrder.LITTLE_ENDIAN);
		
		this.setWidth(bb.getInt());			// 4 bytes
		this.setHeight(bb.getInt());		// 4 bytes
		this.setColorPlane(bb.getShort());	// 2 bytes
		this.setDepth(bb.getShort());		// 2 bytes
		this.setCompression(bb.getInt());	// 4 bytes
		this.setImageSizeWithPadding(bb.getInt());
											// 16
	}
	
	@Override
	public String toString() {
		String txt = "";
		txt += "DIB Header size: " + this.getSize() + "\n";
		txt += "Image Width: " + this.getWidth() + "\n";
		txt += "Image Height: " + this.getHeight() + "\n";
		txt += "RGBColor Depth: " + this.getDepth() + "\n";
		return txt;
	}

	/**
	 * @return the size
	 */
	public int getSize() {
		return size;
	}

	/**
	 * @param size the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return the depth
	 */
	public short getDepth() {
		return depth;
	}

	/**
	 * @param depth the depth to set
	 */
	public void setDepth(short depth) {
		this.depth = (short) Math.round(depth / 8);
	}

	/**
	 * @return the colorPlane
	 */
	public short getColorPlane() {
		return colorPlane;
	}

	/**
	 * @param colorPlane the colorPlane to set
	 */
	public void setColorPlane(short colorPlane) {
		this.colorPlane = colorPlane;
	}

	/**
	 * @return the compression
	 */
	public int getCompression() {
		return compression;
	}

	/**
	 * @param compression the compression to set
	 */
	public void setCompression(int compression) {
		this.compression = compression;
	}

	/**
	 * @return the imageSizeWithPadding
	 */
	public int getImageSizeWithPadding() {
		return imageSizeWithPadding;
	}

	/**
	 * @param imageSizeWithPadding the imageSizeWithPadding to set
	 */
	public void setImageSizeWithPadding(int imageSizeWithPadding) {
		this.imageSizeWithPadding = imageSizeWithPadding;
	}

}
